package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.customer.mapper.ServeRegisterMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.InstitutionRegisterReqDTO;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.customer.service.IserveRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @Author mr.zhang
 * Date 2025/7/1 12:06
 */
@Service
public class ServeRegisterServiceImpl extends ServiceImpl<ServeRegisterMapper, ServeProvider> implements IserveRegisterService {

    @Autowired
    private IServeProviderService iServeProviderService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 机构注册
     * @param dto
     */
    @Override
    public void register(InstitutionRegisterReqDTO dto) {
        //1.判断该手机号是否已被注册
        Integer count = iServeProviderService.lambdaQuery().eq(ServeProvider::getPhone, dto.getPhone()).count();
        if (count>0){
            throw new BadRequestException("该手机号已被注册");
        }
        //2.封装数据,并更新

        String password = bCryptPasswordEncoder.encode(dto.getPassword());

        //新增数据到serve-provider表中,add方法已经新增
        ServeProvider serveProvider = iServeProviderService.add(dto.getPhone(), 3, password);
    }
}
